home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / OT Code Resource / OTGetDefaultEthernetAddress(MW) / OTGetDefaultEthernetAddress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-29  |  3.5 KB  |  146 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        OTGetDefaultEthernetAddress.c
  3.  
  4.     Contains:    Demo of accessing OT from CodeWarrior 68K code resource.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12. */
  13.  
  14. #include <OpenTransport.h>
  15. #include <OpenTptLinks.h>
  16. #include <A4Stuff.h>
  17. #include <HyperXCMD.h>
  18. #include <TextUtils.h>
  19. #include <Strings.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22.  
  23. // Prototypes
  24.  
  25. static void TrueMain(XCmdPtr xpb);
  26. pascal void main(XCmdPtr xpb);
  27.  
  28. // Evil pseudo-LibraryManager stuff
  29.  
  30. typedef long GlobalWorld;
  31.  
  32. GlobalWorld GetCurrentGlobalWorld(void):__D0
  33.     = {0x200D};                                    /* move.l    A5,D0        */
  34.  
  35. void SynchGlobalWorldFromA4(void)
  36.     = {0x2A4C};                                    /* move.l A4,A5        */
  37.  
  38. void SynchA4FromGlobalWorld(void)
  39.     = {0x284D};                                    /* move.l A5,A4        */
  40.  
  41. GlobalWorld SetCurrentGlobalWorld(GlobalWorld:__A0):__D0
  42.     = {0x200D,                                    /* move.l    A5,D0        */
  43.        0x2A48};                                    /* move.l    A0,A5        */
  44.  
  45. // Standard XCMD main entry point
  46.  
  47. pascal void main(XCmdPtr xpb)
  48. {
  49.     long old_A4;
  50.     GlobalWorld old_world;
  51.  
  52.     // Debugger();
  53.     old_world = GetCurrentGlobalWorld();
  54.     
  55.     old_A4 = SetCurrentA4();
  56.     SynchGlobalWorldFromA4();
  57.  
  58.     TrueMain(xpb);
  59.     
  60.     (void) SetCurrentGlobalWorld(old_world);
  61.     (void) SetA4(old_A4);
  62.  
  63.     // Debugger();
  64. }
  65.  
  66. // Generic OT stuff
  67.  
  68. typedef struct {
  69.     unsigned char bytes[6];
  70. } EnetAddress, *EnetAddressPtr;
  71.  
  72. typedef struct T8022Address T8022Address;
  73.  
  74. static T8022Address simple_addr = {
  75.     AF_8022,                                                            // OTAddressType for 802 provider
  76.     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},    // hardware address, use zeros for bind requests
  77.     0x8888,                                                                // SAP / protocol type, value > 1500 implies ethernet protocol with no SNAP
  78.   {0x00,0x00,0x00,0x00,0x00}
  79. };
  80.  
  81. static OSStatus GetDefaultEthernetAddress(EnetAddressPtr def_enet_addr)
  82. {
  83.   EndpointRef ep;
  84.   OSStatus    err;
  85.   TBind       bind_request;
  86.   TBind       bind_result;
  87.   T8022Address  theReturnAddr;
  88.  
  89.   ep = OTOpenEndpoint(OTCreateConfiguration(kEnetName), 0, 0, &err);
  90.   if (err == kOTNoError) {
  91.     // finish bind request
  92.     bind_request.addr.buf = (UInt8 *) &simple_addr;
  93.     bind_request.addr.len = k8022BasicAddressLength;  // family type + Ethernet + type field
  94.     bind_request.addr.maxlen = 0;     
  95.     bind_request.qlen = 0;
  96.     // setup bind result
  97.     bind_result.addr.buf = (UInt8 *) &theReturnAddr;
  98.     bind_result.addr.len = 0;
  99.     bind_result.addr.maxlen = sizeof(theReturnAddr);
  100.     bind_result.qlen = 0;
  101.     // call bind
  102.     err = OTBind(ep, &bind_request, &bind_result);
  103.     if (err == noErr) {
  104.             BlockMove((Ptr) theReturnAddr.fHWAddr, (Ptr) def_enet_addr->bytes, 6);
  105.       // clean up
  106.       err = OTUnbind(ep);
  107.     }
  108.     err = OTCloseProvider(ep);
  109.   }
  110.   return (err);
  111. }
  112.  
  113. static void TrueMain(XCmdPtr xpb)
  114.     // Extra procedure wrapper to avoid horrible register caching problem
  115.     //  whereby the value of result was held in register A3 which was
  116.     //  setup from an A5 relative offset before our A5 had been set up
  117.     //  by InitCodeResource.  Urgh!
  118. {
  119.     OSStatus err;
  120.     EnetAddress def_enet_addr;
  121.     char result[256];
  122.     
  123.     err = InitOpenTransport();
  124.     if (err == noErr) {
  125.         err = GetDefaultEthernetAddress(&def_enet_addr);
  126.         if (err == noErr) {
  127.             sprintf(result, "%02X:%02X:%02X:%02X:%02X:%02X",
  128.                 def_enet_addr.bytes[0],
  129.                 def_enet_addr.bytes[1],
  130.                 def_enet_addr.bytes[2],
  131.                 def_enet_addr.bytes[3],
  132.                 def_enet_addr.bytes[4],
  133.                 def_enet_addr.bytes[5]
  134.             );
  135.         };
  136.         
  137.         CloseOpenTransport();
  138.     };
  139.     
  140.     if (err != noErr) {
  141.         strcpy(result, "Error");
  142.     };
  143.  
  144.     (void) PtrToHand((Ptr) result, &xpb->returnValue, strlen(result) + 1);
  145. }
  146.